clear all
set more off
set maxvar 10000


    file open Drops1 using "$Mydirectory2/appendix_e/trackingdrops_1.tex", write replace
        foreach s in anes avtmh57 avtmh76 gss nfs nlsmw nlsom nlsy79 nlsym66 nlsyw68 nsba nsfh ocg62 ocg73 psid {
       
    *-------------------------------------------*
    *  STEP 1: DEFINE LOCALS + FIX PSID  
    *-------------------------------------------*
        if "`s'"=="anes" {
            local name "American National Election Survey, 1956--1970"
            local f "ANES"
            local subf2 "output"
            local dta2 "ANES56to70_analysis.dta"
        }            
        if "`s'"=="avtmh57" {
            local name "Americans View Their Mental Health, 1957"
            local f "Americans_MentalHealth_1957"
            local subf2 "output"
            local dta2 "AVTMH1957_IGEanalysis.dta"
        }
        if "`s'"=="avtmh76" {
            local name "Americans View Their Mental Health, 1976"
            local f "Americans_MentalHealth_1976"  
            local subf2 "output"
            local dta2 "AVTMH1976_cleaned.dta"
        }
        if "`s'"=="gss" {
            local name "General Social Surveys, 1977--2018"
            local f "GSS"
            local subf2 "output"
            local dta2 "GSS_allyears_foranalysis.dta"
        }
        if "`s'"=="nfs" {
            local name "National Fertility Study, 1970"
            local f "FertilityStudy_1970"
            local subf2 "output"
            local dta2 "NFS70_analysis.dta"
        }
        if "`s'"=="nlsmw" {
            local name "National Longitudinal Survey of Mature Women, 1967"
            local f "NLS_MatureWomen"
            local subf2 "output"
            local dta2 "NLSMW_analysis.dta"
        }
        if "`s'"=="nlsom" {
            local name "National Longitudinal Survey of Older Men, 1966"
            local f "NLS_older_men"
            local subf2 "output"
            local dta2 "nls_older_cleaned"
        }
        if "`s'"=="nlsy79" {
            local name "National Longitudinal Survey of Youth, 2002"
            local f "NLSY79"
            local subf2 "wrkdata"
            local dta2 "nlsy79_newxsec.dta"
        }
        if "`s'"=="nlsym66" {
            local name "National Longitudinal Survey of Young Men, 1981"
            local f "NLSYM66"
            local subf2 "wrkdata"
            local dta2 "nlsym66_newxsec.dta"
        }
        if "`s'"=="nlsyw68" {
            local name "National Longitudinal Survey of Young Women, 1988"
            local f "NLSYW68"
            local subf2 "wrkdata"
            local dta2 "nlsyw68_newxsec.dta"
        }
        if "`s'"=="nsba" {
            local name "National Survey of Black Americans, 1979--1980"
            local f "NSBA"
            local subf2 "output"
            local dta2 "cleaned_nsba.dta"
        }
        if "`s'"=="nsfh" {
            local name "National Survey of Families \& Households, 1987--1988"
            local f "natl_survey_fam_HH"
            local subf2 "output"
            local dta2 "fam_hh_cleaned.dta"
        }
        if "`s'"=="ocg62" {
            local name "Occupational Changes in a Generation, 1962"
            local f "CPS_1962_1973"
            local subf2 "output"
            local dta2 "OCG62_IGEanalysis.dta"
        }
        if "`s'"=="ocg73" {
            local name "Occupational Changes in a Generation, 1973"
            local f "CPS_1962_1973"
            local subf2 "output"
            local dta2 "OCG73_IGEanalysis.dta"
        }

        /*Note: The PSID requires slightly more work, as it is a 
                combo of the 1997 and 2017 cross-sections */
        if "`s'"=="psid" {

            local name "Panel Study of Income Dynamics, 1997 \& 2017"             

            use "$Mydirectory1/3_Output/1_PooledData.dta", clear
            keep if data=="psid1997" | data=="psid2017"
            replace data="psid" if (data=="psid1997" | data=="psid2017") 
            
            tempfile cleanedpsid
            save `cleanedpsid'            
        }

    *---------------------------------------------------*
    /*  COLUMN 1: GRAB # RESPONDENTS LEFT AFTER AGE 
                  AND NATIVE-BORN RESTRICTIONS ARE 
                  IMPLEMENTED */
    *---------------------------------------------------*
        * Bring in cleaned data
            if "`s'"!= "psid" use "$Mydirectory1/1_DataSources/`f'/`subf2'/`dta2'" , clear
            else use `cleanedpsid'

        * Count # of respondents left in each dataset, then reformat
            count
            local cleaned = `r(N)'
            local cleaned2: display %-09.0fc `cleaned'

    *---------------------------------------------------*
    /*  COLUMN 2: GRAB # RESPONDENTS LEFT AFTER 
                  ADJUSTING FOR WEIGHT AND COHORT 
                  CONSIDERATIONS */
    *---------------------------------------------------*

        * Bring in cleaned, appended data
		use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 

        * Count # of respondents left in each dataset, then reformat
            count if data=="`s'"
            local adjusted = `r(N)'
            local adjusted2: display %-09.0fc `adjusted'

    *---------------------------------------------------*
    /*  COLUMN 3: GRAB # RESPONDENTS LEFT AFTER 
                  RESTRICTING TO BASELINE SAMPLE
                  (I.E., NON-MISSING INFO ON FAMILY 
                  INCOME AND FATHER OCCUPATION) */
    *---------------------------------------------------*
        
        /*Count # of respondents per dataset who are in 
          baseline sample */
            keep if baseline_sample==1
            count if data=="`s'"
            local bsample = `r(N)'
            local bsample2: display %-09.0fc `bsample'
       
            file write Drops1 "`name' & `cleaned2' & `adjusted2' & `bsample2' \\"  _n

        } 

    file close Drops1